/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.utils;

import java.util.LinkedHashMap;

/**
 * Log message instance.
 * <p>
 * Helper for logging message and a set of parameters.
 * 
 * @author Jan Pastor 
 */
public class LogMsg {

    private final String _msg;

    // ----------------------------------------------------------------------

    /**
     * Creates log message instance.
     * <p>
     * Example of use:<br>
     * <code>log.debug(new LogMsg("Result", "id", id, "name", name));</code>
     * @param msg log message text.
     * @param params a list of log entry parameters, where even are parameters
     *            names, and odd are parameter values.
     */
    public LogMsg(String msg, Object... params) {
        _msg = toString(msg, params);
    }

    // ----------------------------------------------------------------------

    /**
     * Returns log message transformed to one string.
     * @return log message transformed to one string.
     */
    public String toString() {
        return _msg;
    }

    /**
     * Directly creates log message as string.
     * <p>
     * Example of use:<br>
     * <code>log.debug(LogMsg.toString("Result", "id", id, "name", name));</code>
     * @param msg log message text.
     * @param params a list of log entry parameters, where even are parameters
     *            names, and odd are parameter values.
     * @return log message as string.
     */
    public static String toString(String msg, Object... params) {
        if (params == null || params.length == 0) {
            return msg;
        } else {
            LinkedHashMap map = CollectionUtils.paramsToMap(params);
            return msg + " (" + CollectionUtils.mapToString(map, ": ", ", ", true) + ")";
        }
    }

    /**
     * Creates log message with given parameters, where message is 'Input'.
     * <p>See {@link #toString(String, Object[])} for additional details.
     * @param params
     * @return complete message.
     */
    public static String inp(Object... params) {
        return toString("Input", params);
    }

    /**
     * Creates log message with given parameters, where message is 'Output'.
     * <p>See {@link #toString(String, Object[])} for additional details.
     * @param params
     * @return complete message.
     */
    public static String out(Object... params) {
        return toString("Output", params);
    }

}
